{extend name="template/base" /}
{block name="content"}
<div class="page-container">
    {include file="nav" /}
<div class="toptip">
    <h2>提示：</h2>
    <p>
      建立总题库并命名，选择所属上级题库，状态默认启用，文本框内输入抽题规则。星号栏为必填<br>
    </p>
</div>
 <legend><i class="Hui-iconfont">&#xe600;</i>题库管理</legend>
    <form class="form form-horizontal" id="form" method="post" action="{:\\think\\Request::instance()->baseUrl()}">
        <input type="hidden" name="id" value="{$vo.id ?? ''}">
        <!-- <input type="hidden" name="alltype" id="alltypeid" value="{$vo.alltype ?? ''}" style="display:{if $vo.parentid==0}none{else}block{/if}"> <!-- 查询出顶级题库alltype 规则 -->
		<div class="row cl">
            <label class="form-label  f-l ml-20">题库名：</label>
            <div class="formControls col-xs-8 col-sm-5">
                <input type="text" class="input-text" placeholder="题库名" name="name" value="{$vo.name ?? ''}" datatype="*" nullmsg="请输入题库名称，题库名称不能为空">
            </div>
            <div class="col-xs-offset-4"></div>
        </div>
        <div class="row cl">
            <label class="form-label  f-l ml-20">上级题库：</label>
            <div class="formControls  col-xs-8 col-sm-5">
                <div class="select-box">
                    <select name="parentid" id='parentid' class="select" datatype="*" nullmsg="请选择所属上级题库" onchange="ajaxGetToplist($(this).val(),{$vo.id ?? '0'});">
						<option value="0" {if condition="$vo.parentid eq 0"} selected {/if} >顶级题库</option>
                        {volist name='qbanks' id='st'}
                        <option value="{$st.id}"  {$vo.parentid==$st.id ? 'selected' : ''}>{$st.name}</option>
                        {/volist}
                    </select>
                </div>
            </div>
            <div class="col-xs-offset-4"></div>
        </div>
            <div class="row cl" id="toplist" style="display:{if $vo.parentid==0}block{else}none{/if}">
                <label for="subject" class="form-label f-l ml-20">题库题型：</label>
                <div class="col-xs-8 col-sm-6 skin-minimal"> 
                    {volist name="question_type" id="questype"}  
                    <div class="check-box formControls">
                        <input type="checkbox" id ="T{$questype['id']}" name="alltype[questypes][{$questype['id']}]" value="1" {if isset($vo_ratio['questypes'][$questype['id']]) && $vo_ratio['questypes'][$questype['id']]==1}checked{/if}/> 
                        <label for="T{$questype['id']}">{$questype['type_name']}</label>
                    </div>
                    {/volist} 
                    <span class="span">（修改顶级题库题型可能会影响到分题库的题型选择！）</span>
                </div>

            </div>  

        <div class="row cl" id="creditsettingid" style="display:{if $vo.parentid==0}block{else}none{/if}">
            <label class="form-label  f-l ml-20">是否开启学分：</label>
             <div class="formControls col-xs-8 col-sm-5 skin-minimal">
                <div class="radio-box">
                    <input type="radio" name="creditsettingid" id="radio-0" value="1" onclick="$('#scores').show();" {if condition="$vo.creditsettingid eq 1"} checked {/if}>
                    <label for="radio-0">启用学分</label>
                </div>
                <div class="radio-box">
                    <input type="radio" name="creditsettingid" id="radio-1" value="0" onclick="$('#scores').hide();" {if condition="$vo.creditsettingid neq 1"} checked {/if}>
                    <label for="radio-1">禁用学分</label>
                </div>
            </div>
            <div class="col-xs-3 col-sm-3"></div>
        </div>
        <div class="row cl" id="scores" style="display:{if $vo.creditsettingid==1 && $vo.parentid==0}block{else}none{/if}">
            <label class="form-label  f-l ml-20">学分设置：</label>
            <div class="formControls col-xs-8 col-sm-5">
                <input type="text" class="input-text" value="{$vo.scores ?? ''}"  name="scores" nullmsg="学分设置">
            </div>
            <div class="col-xs-3 col-sm-3"></div>
        </div>
		<div class="row cl">
            <label class="form-label  f-l ml-20">题库封面：</label>
            <div class="formControls col-xs-8 col-sm-5">
				<!-----图片上传----->
				<input type="text" class="input-text" name="thumb" id="upload" placeholder="请点击后面的上传按钮"  style="width: 70%" value="{$vo.thumb ?? '/static/mobile/images/qbank.png'}" >
                <button type="button" class="btn btn-primary radius" onclick="layer_open('文件上传','{:\\think\\Url::build(\'Upload/index\', [\'id\' => \'upload\'])}')">上传</button>
				<!--<a onclick="$(this).attr('href', $('#upload').val())" type="button" class="btn btn-success radius" data-lightbox="preview">预览</a>-->
                <span class="span">noimage.gif为默认图片，请上传您所需要的图片</span>
				<!-----图片上传 end----->
                {if condition="$vo.thumb"}
                <p><img type="text"  placeholder="上传文件" id='upload_imgx1' src="{$vo.thumb ?? ''}" width="200px" height="150px" ></p>
                {/if}
            </div>
            <div class="col-xs-3 col-sm-3"></div>
        </div>
        <div class="row cl">
            <label class="form-label  f-l ml-20">共享题库：</label>
            <div class="formControls col-xs-8 col-sm-5 skin-minimal">
                <div class="radio-box">
                    <input type="radio" name="issharing" id="issharing-1" value="1" datatype="*" nullmsg="请填写状态" ignore="ignore" {if condition="$vo.issharing neq 0"} checked {/if}>
                    <label for="issharing-1">共享</label>
                </div>
                <div class="radio-box">
                    <input type="radio" name="issharing" id="issharing-0" value="0" datatype="*" nullmsg="请填写状态" ignore="ignore" {if condition="$vo.issharing neq 1"} checked {/if}>
                    <label for="issharing-0">不共享</label>
                </div>
                <span>（共享后子平台也可看见该题库）</span>
            </div>
            <div class="col-xs-offset-4"></div>
        </div>
        <div class="row cl">
            <label class="form-label  f-l ml-20">状&nbsp;&nbsp;&nbsp;态：</label>
            <div class="formControls col-xs-8 col-sm-5 skin-minimal">
                <div class="radio-box">
                    <input type="radio" name="status" id="status-1" value="1" datatype="*" nullmsg="请填写状态" ignore="ignore" {if condition="$vo.status neq 0"} checked {/if}>
                    <label for="status-1">启用</label>
                </div>
                <div class="radio-box">
                    <input type="radio" name="status" id="status-0" value="0" datatype="*" nullmsg="请填写状态" ignore="ignore" {if condition="$vo.status neq 1"} checked {/if}>
                    <label for="status-0">禁用</label>
                </div>
            </div>
            <div class="col-xs-offset-4"></div>
        </div>
		<!-- <div class="row cl">
            <label class="form-label  f-l ml-20">规&nbsp;&nbsp;&nbsp;则：</label>
            <div class="formControls col-xs-8 col-sm-5">
                <input type="text" class="input-text" placeholder="规则" name="rule" value="{$vo.rule ?? ''}" datatype="*" nullmsg="请输入规则，规则名称不能为空">
            </div>
            <div class="col-xs-offset-4"></div>
        </div> -->
		<div class="row cl">
            <label class="form-label  f-l ml-20">排&nbsp;&nbsp;&nbsp;序：</label>
            <div class="formControls col-xs-8 col-sm-5">
                <input type="text" class="input-text" placeholder="排序" name="sort" value="{$vo.sort ?? '50'}" datatype="*" nullmsg="请输入排序，排序名称不能为空">
            </div>
            <div class="col-xs-offset-4"></div>
        </div>
		<!--<div class="row cl">-->
            <!--<label class="form-label  f-l ml-20">抽题规则：</label>-->
            <!--<div class="formControls col-xs-8 col-sm-5">-->
				<!--<textarea class="textarea" placeholder="" name="remark"  onkeyup="$.Huitextarealength(this,500)" aria-required="true" aria-invalid="true">{$vo.ratio ?? ''}</textarea>-->
            <!--</div>-->
            <!--<div class="col-xs-offset-4"></div>			-->
        <!--</div>-->
        <div class="rule_show {if $vo}{if $vo.parentid==0}hide{else}show{/if}{else}hide{/if}">
            <div class="row cl">
                <label class="form-label f-l ml-20">题型</label>
                <div class="formControls col-xs-6 col-sm-6 ">
                    <div class="f-l select-combox">
                        <select id="p_setting_3" class="select-default" >
                            <option value="" data-num="0" >请选择题型</option>
                            {volist name="question_type" id="qt"} 
                            {if isset($vo_ratio['questypes'][$qt['id']]) && $vo_ratio['questypes'][$qt['id']]==1}
                            <option value="{$qt.id}" >{$qt.type_name}</option>
                            {/if}
                            {/volist}
                        </select>
                    </div>

                    <div class="f-l select-combox ml-10">
                        <select id="p_cid" class="select-default combox" ref="secs" refUrl="/admin/common_ajax/ajaxGetSections/subjectid/{value}/sectionid/{value}/" callback="getSectionContent">
                            <option value="" {$vo.p_cid ?? ''}>请选择科目</option>
                            {volist name="subject" id="subc"}
                            <option value="{$subc['id']}" {$vo.p_cid==$subc['id'] ? 'selected' : ''}>{$subc['s_name']}</option>
                            {/volist}
                        </select>
                    </div>
                    <div class="f-l select-combox ml-10">
                        <select id="secs" class="select-default combox" ref="know_3" refUrl="/admin/common_ajax/ajaxGetknows/subjectid/0/sectionid/{value}/" callback="getSectionContent">
                            <option value="">请选择章节</option>
                            {volist name="sections" id="val"}
                            <option value="{$val.id}">{$val.se_name}</option>
                            {/volist}
                        </select>
                    </div>
                    <div class="f-l ml-10">
                        <select id="know_3" class="select-default">
                            <option value="">请选择知识点</option>
                        </select>
                    </div>
                    <div class="f-l ml-10">
                        <input id="paperAdd" type="button" class="btn btn-primary radius" value="添加" />
                    </div>
                    <!-- 		<div class="f-l ml-10">
                                <a class="label label-warning radius mt-5 f-12" href="javascript:;" onclick="layer_open('execl导入', $('#modeplane').html($('#advancedmode').html()),{type:1})"><i class="Hui-iconfont"></i> 试题导入</a>
                            </div>	 -->
                </div>
            </div>

            <div class="row cl">
                <label class="form-label f-l ml-20">抽题数量</label>
                <div class="formControls col-xs-6 col-sm-6 ">
                    <input class="input-text" style="width:20%" maxlength="6" id="p_setting_4" name="p_setting[][]" value="" type="text">&nbsp;&nbsp;题，
                    每题&nbsp;&nbsp;<input class="input-text" style="width:20%" maxlength="6" id="sett_3" name="p_setting[][]" value="" type="text">&nbsp;&nbsp;分
                </div>

            </div>
            <div class="row cl">
                <label class="form-label f-l ml-20">您已添加：</label>
                <div id="ruleShow" class=" col-xs-6 col-sm-6 ">
                    {if $u_ratio}
                    {volist name="u_ratio" id="val"}
                    <p class='paper-rule' data-total='{$val[2]}' data-rule='{$val[4]}'>
                        {$KnowsList[$val[0]]['k_name']}{$question_type[$val[1]]['type_name']} {$val[2]}道,每题{$val[3]}分
                        <i onclick='deleteRule(this);'>删除</i></p>
                    {/volist}
                    {/if}
                </div>
            </div>
            <input type="hidden" name="data_rule[]"  id="data_rule" value="{$vo.ratio??''}">
        </div>


        
        <div class="row cl">
            <label class="form-label f-l ml-20">描述：</label>
            <div class="formControls col-xs-8 col-sm-5">
                <textarea class="textarea" placeholder="" name="remark" onKeyUp="textarealength(this,100)">{$vo.se_describe ?? ''}</textarea>
                <p class="textarea-numberbar"><em class="textarea-length">0</em>/100</p>
            </div>
             <div class="col-xs-offset-4"></div>
        </div>

       <div class="row cl">
            <div class="ml-save">
                <button type="submit" class="btn btn-primary radius">&nbsp;&nbsp;保存&nbsp;&nbsp;</button>
                <button type="button" class="btn btn-default radius ml-20" onClick="layer_close();">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
            </div>
        </div>
    </form>
</div>
{/block}
{block name="script"}
<style>
    .paper-rule i{
        font-style: normal;
        color: #f00;
        font-size: 12px;
    }
</style>
<script>
    // 分题库不用选择题型 继承顶级题库类型 并ajax获取顶级题库类型
    function ajaxGetToplist($val,$cid){
        // 顶级题库隐藏下级科目知识点
        if ($val==0){
            $('#creditsettingid').show();$('.rule_show').hide();
            $('#toplist').show();
            // $('#alltypeid').css({"display":"none","disabled",true});
        }else {
            $('#creditsettingid').hide();$('.rule_show').show();
            $('#toplist').hide();
            // $('#alltypeid').css({"display":"block","disabled",false});
            //获取二级选择
            ajaxChildrenList($val,$cid);
        } 
        inherit();

    }
    // ajax加载分题库的题型选择
    function ajaxChildrenList(id,cid){
        $("#p_setting_3").attr("disabled", true);
        $.post("/admin/qbank/ajaxGetChildrenlist.html",
            {pid:id,id:cid},
            function(data){
                if(data!=''){
                    sjson = JSON.parse(data);
                    $("#p_setting_3").empty();
                        $("#p_setting_3").append("<option value='' >请选择题型</option>");
                    for( var i = 0; i < sjson.length; i++){ 
                        $("#p_setting_3").append("<option value='"+sjson[i].id+"' >"+sjson[i].type_name+"</option>");
                    }
                }
                $("#p_setting_3").attr("disabled",false);
            });
    }
    function writeRule() {
        var data_rule = [];
        $('#ruleShow p').each(function () {
            data_rule.push($(this).attr('data-rule'));
        });
        $('#data_rule').val(data_rule);
    }
    $(document).ready(function(){
        common();
        $("#paperAdd").on("click", function(){
            var secs_3Text = $("#secs_3 option:selected").text();
            var know_3 = $("#know_3 option:selected").val();
            var know_3Text = $("#know_3 option:selected").text();
            var p_setting_3 = $("#p_setting_3 option:selected").val();
            var p_setting_3Text = $("#p_setting_3 option:selected").text();
            var num = $("#p_setting_4").val();
            var score = $("#sett_3").val();
            var rule = "";
            var total = 0;
            var html = "";
            console.log(know_3);
            console.log(p_setting_3);
            console.log(num);
            console.log(score);
            if(know_3 && p_setting_3 && num && score) {
                rule = know_3 + ":" + p_setting_3 + ":" + num + ":" + score;
                total = parseInt(num)*parseInt(score);
                var ruleString = secs_3Text + know_3Text + p_setting_3Text + num + "道,每题" + score + "分";

                html = "<p class='paper-rule' data-total='" + total
                            + "' data-rule='" + rule
                        + "'>" + ruleString
                        + "<i onclick='deleteRule(this);'>删除</i></p>";
                $("#ruleShow").append(html);
                writeRule();
            }else{
                layer_msg = layer.msg('规则不正确…', {time: 2000});
                return false;
            }
        })
    });
    function getToalScore() {
        var total = 0;
        $('#ruleShow p').each(function () {
            total +=parseInt($(this).attr('data-total'));
        });
        return total;
    }
    function deleteRule(obj){
        $(obj).parent().remove();
        writeRule();
    }
	
	
	function inherit(){
		var id = $('#parentid').val();
		var sjson = '';
		$.post("/admin/qbank/inherits.html",
			{qbankid:id},
			function(data){
				if(data!=''){
					sjson = JSON.parse(data);
					$("#p_cid").empty();
						$("#p_cid").append("<option value='' >请选择科目</option>");
					for( var i = 0; i < sjson.length; i++){
						

						$("#p_cid").append("<option value='"+sjson[i].id+"' >"+sjson[i].s_name+"</option>");
					}
				}
			});
	}
</script>
<script type="text/javascript" src="__LIB__/Validform/5.3.2/Validform.min.js"></script>
<script>
    $(function () {
        $("[name='status'][value='{$vo.status ?? ''}']").prop("checked", true);

        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });

        $("#form").Validform({
            tiptype: 2,
            ajaxPost: true,
            showAllError: false,
            callback: function (ret){
                ajax_progress(ret);
            }
        });
    })
</script>
{/block}
